<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${classAttribute.packagePath}.dao.${classAttribute.className}Mapper">

    <!-- 实体类映射-->
    <resultMap type="${classAttribute.packagePath}.entity.po.${classAttribute.className}" id="${classAttribute.className}ResultMap">
        <#list fieldAttributeList as column>
            <result column="${column.columnName}" property="${column.fileName}"/>
        </#list>
    </resultMap>
   <!-- 按条件查询-->
    <select id="findAll" parameterType="${classAttribute.packagePath}.entity.po.${classAttribute.className}" resultMap="${classAttribute.className}ResultMap" >
    	select
    	    <#list fieldAttributeList as column><#if column_has_next>${column.columnName},<#else>${column.columnName}</#if></#list>
    	 from ${classAttribute.tableName}
    		<where>
    		    <#list fieldAttributeList as column>
    		        <if test="${column.fileName}!=null and ${column.fileName}!=''">
                       and  ${column.columnName}=${r'#'}{${column.fileName}}
                     </if>
    		     </#list>

             </where>
    </select>
    <!-- 查询单个-->
    <select id="get${classAttribute.className}" parameterType="java.lang.Long" resultMap="${classAttribute.className}ResultMap" >
        select
        	 <#list fieldAttributeList as column><#if column_has_next>${column.columnName},<#else>${column.columnName}</#if></#list>
        from ${classAttribute.tableName}
        where <#if keys?size &gt; 0 ><#list keys as k><#list fieldAttributeList as column><#if k_index==0 && k.col==column.columnName> ${column.columnName}=${r'#'}{${column.fileName}}<#if k_has_next> and </#if></#if></#list> </#list></#if>
   </select>



   <!-- 保存-->
    <insert id="save${classAttribute.className}"  <#if keys?size &gt; 0 >useGeneratedKeys="true" keyProperty="<#list keys as k><#if k_index==0>${k.fil}</#if></#list>"</#if>  parameterType="${classAttribute.packagePath}.entity.po.${classAttribute.className}">
        INSERT INTO  ${classAttribute.tableName}
        	(
                <#list fieldAttributeList as column><#if column_has_next>${column.columnName},<#else>${column.columnName}</#if></#list>
        	)
        VALUES
        	(
                <#list fieldAttributeList as column><#if column.columnName=="gmt_create" || column.columnName=="gmt_modified">NOW()<#else>${r'#'}{${column.fileName}}</#if><#if column_has_next>,</#if></#list>
        	)
     </insert>

      <!-- 批量保存-->
       <insert id="saveBatchTrip${classAttribute.className}"  <#if keys?size &gt; 0 >useGeneratedKeys="true" keyProperty="<#list keys as k><#if k_index==0>${k.fil}</#if></#list>"</#if>  parameterType="java.util.List">
          INSERT INTO  ${classAttribute.tableName}
             (
                  <#list fieldAttributeList as column><#if column_has_next>${column.columnName},<#else>${column.columnName}</#if></#list>
             )
           VALUES
           <foreach collection="list" index="index" item="item" separator=",">
             (
                  <#list fieldAttributeList as column><#if column.columnName=="gmt_create" || column.columnName=="gmt_modified">NOW()<#else>${r'#'}{item.${column.fileName}}</#if><#if column_has_next>,</#if></#list>
             )
           </foreach>
        </insert>

    <!-- 修改-->
     <update id="update${classAttribute.className}" parameterType="${classAttribute.packagePath}.entity.po.${classAttribute.className}">

        UPDATE${classAttribute.tableName}
            <trim prefix="set" suffixOverrides=",">
                <#list fieldAttributeList as column><#if column.columnName=="gmt_create"><#elseif column.columnName=="gmt_modified"> ${column.columnName}=NOW()<#if column_has_next>,</#if><#else> ${column.columnName}=${r'#'}{${column.fileName}}<#if column_has_next>,</#if></#if></#list>
            </trim>
        WHERE
            <#if keys?size &gt; 0 > <#list keys as k> <#list fieldAttributeList as column> <#if k.col==column.columnName> ${column.columnName}=${r'#'}{${column.fileName}}<#if k_has_next> and </#if></#if></#list> </#list></#if>

     </update>

      <!-- 删除-->
       <delete id="delete${classAttribute.className}" parameterType="java.lang.Long">
           DELETE FROM ${classAttribute.tableName}
           WHERE <#if keys?size &gt; 0 ><#list keys as k><#list fieldAttributeList as column><#if k_index==0 && k.col==column.columnName> ${column.columnName}=${r'#'}{${column.fileName}}<#if k_has_next> and </#if></#if></#list> </#list></#if>
       </delete>

        <!-- 批量删除-->
       <delete id="deleteBatch${classAttribute.className}" parameterType="java.util.List">
           	DELETE FROM ${classAttribute.tableName}
           	WHERE <#if keys?size &gt; 0 > <#list keys as k> <#list fieldAttributeList as column> <#if  k_index==0 &&  k.col==column.columnName> ${column.columnName} in </#if></#list></#list></#if>
           	<foreach collection="list" index="index" item="ids" open="(" separator="," close=")">
           		${r'#'}{ids}
           	</foreach>
       </delete>
</mapper>